﻿(function () {
    "use strict";

    F.GraphicsManager = function () {
        var self = this;
        self.timeInterval = 25;
        self.gameTimeAcceleration = 4; //Ускорение игрового времени
        self.shapes = [];
        self.stopProcess = false;
        self.refreshProcess = false;
    };

    var gmp = F.GraphicsManager.prototype;

    gmp.registerShape = function (shape) {
        if (!shape)
            throw "Logical error. Null object.";
        var self = this;
        self.shapes.push(shape);
    };

    gmp.unregisterShape = function (shape) {
        if (!shape)
            throw "Logical error. Null object.";
        var self = this;
        self.refreshProcess = self.stopProcess = true;
        for (var i = 0, length = self.shapes.length; i < length; i++) {
            if (self.shapes[i] === shape) {
                self.shapes.splice(i, 1);
                break;
            }
        }
        self.stopProcess = false;
    };

    gmp.initialize = function () {
        var self = this;
        setInterval(function () {
            self.process.apply(self);
        }, self.timeInterval);
    };

    gmp.process = function () {
        var self = this;
        for (var i = 0, length = self.shapes.length; i < length; i++) {
            if (self.refreshProcess){
                self.refreshProcess = false;
                return;
            }
            self.shapes[i].process(self.gameTimeAcceleration * self.timeInterval / 1000); //Выражаем время в секундах
        }
    };

    gmp = null;

    window.GraphicsManager = new F.GraphicsManager();
} ());